
import java.awt.Color;

public class PercolationVisualizer {
    private boolean[][] isPaintedOpen;
    private boolean[][] isPaintedFull;
    private Percolation per;

    //Construct
    public PercolationVisualizer(Percolation per) {
        this.per = per;
        this.isPaintedFull = new boolean[per.getN()][per.getN()];
        this.isPaintedOpen = new boolean[per.getN()][per.getN()];
        //Fill it all black
        StdDraw.setPenColor(StdDraw.BLACK);
        StdDraw.filledRectangle(0.5, 0.5, 1.0, 1.0);
    }

    // Draw the percolation viualization
    public void draw() {
        //variables
        int N = per.getN();
        double radius = 1.0 / (N * 2);
        double border = 0.05 / N;


        //Paint squares that are open or full
        StdDraw.show(0);
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++) {
                if (per.isFull(x, y)) {
                    if(!this.isPaintedFull[x][y]){
                        StdDraw.setPenColor(StdDraw.BLUE);
                        StdDraw.filledSquare(radius + x * radius * 2, radius + y * radius * 2, radius - border);
                        this.isPaintedFull[x][y] = true;
                    }
                } else if (per.isOpen(x, y)) {
                    if(!this.isPaintedOpen[x][y]){
                        StdDraw.setPenColor(StdDraw.WHITE);
                        StdDraw.filledSquare(radius + x * radius * 2, radius + y * radius * 2, radius - border);
                        this.isPaintedOpen[x][y] = true;
                    }
                }
            }
        }
        StdDraw.show(0);
    }
}
